package com.oncliedisk.administrator.ui;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import android.support.v7.app.AppCompatActivity;

import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.mogujie.tt.R;
import com.oncliedisk.administrator.adapter.FragPagerAdapter;
import com.oncliedisk.administrator.fragment.FragMentFile;
import com.oncliedisk.administrator.fragment.FragMentTransmission;

import com.oncliedisk.administrator.view.SegmentedGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页面，实现文件和传输界面
 */
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    private SegmentedGroup mGroup; //选项卡
    private ViewPager mPager; //ViewPager
    private List<Fragment> list;
    private FragMentFile  file;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.oncliedisk_activity_main);
        initWeight();
        initListener();
        initData();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        //组装FragMent
        file  = new FragMentFile();
        list.add(file);
        list.add(new FragMentTransmission());
        //设置到ViewPager中
        mPager.setAdapter(new FragPagerAdapter(getSupportFragmentManager(), list));
    }

    /**
     * 初始化监听
     */
    private void initListener() {
        mGroup.setOnCheckedChangeListener(this);
        mPager.setOnPageChangeListener(this);
    }

    /**
     * 初始化控件
     */
    private void initWeight() {
        mGroup = (SegmentedGroup) findViewById(R.id.main_group);
        mPager = (ViewPager) findViewById(R.id.main_viewpager);
        list = new ArrayList<Fragment>();
    }

    /**
     * 当选项卡改变时触发
     * @param group
     * @param checkedId
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        //给选中的RadioButton设置选中属性,给没有选中的设置false
        switch (checkedId){
            case R.id.main_radio_file:
            //点击了文件
            mPager.setCurrentItem(0);
                break;
            case R.id.main_radio_input:
             //点击了传输
             mPager.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    /**
     * 当Pager页面改变后
     * @param position
     */
    @Override
    public void onPageSelected(int position) {
        ((RadioButton)mGroup.getChildAt(position)).setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean b = file.onKeyDown(keyCode, event);
        if(b == false){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
